package gui;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPanel;

import observer.Broker;
import observer.Observer;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import java.awt.Font;
import java.awt.Insets;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFormattedTextField;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JOptionPane;

public class StatisticheNavigatoriPanel extends JPanel implements ActionListener,Observer{

	
	private Broker broker;
	
	
	private JLabel lblDatiNavigatori,lblNumeroNavigatori,lblSelezionaNavigatore,lblNumeroIncroci,lblNumeroStrade,lblNumeroAuto;
	private JFormattedTextField numNavigatoriTextField,numIncrociTextField,numStradeTextField,numAutoTextField;
	private JComboBox comboBox;
	private JButton btnGetdatanavigatore;
	private Icon getDateImage;
	private JLabel lblNavigatore;
	private JFormattedTextField nomeNavigatoreField;
	
	
	public StatisticheNavigatoriPanel(Broker broker) {
		// TODO Auto-generated constructor stub
		
		this.getDateImage = new ImageIcon("img/refreshData.jpg");
		this.broker=broker;
		broker.addObserver(this);
		this.setPreferredSize(new Dimension(250, 600));
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 0, 38, 38, 0, 0};
		gridBagLayout.rowHeights = new int[]{32, 0, 0, 58, 0, 115, 0, 46, 0, 45, 0, 45, 0, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 0.0, 0.0, 1.0, 0.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);
		
		lblDatiNavigatori = new JLabel("Dati navigatori");
		lblDatiNavigatori.setFont(new Font("Tahoma", Font.BOLD, 13));
		GridBagConstraints gbc_lblDatiNavigatori = new GridBagConstraints();
		gbc_lblDatiNavigatori.insets = new Insets(0, 0, 5, 0);
		gbc_lblDatiNavigatori.gridwidth = 5;
		gbc_lblDatiNavigatori.gridx = 0;
		gbc_lblDatiNavigatori.gridy = 0;
		add(lblDatiNavigatori, gbc_lblDatiNavigatori);
		
		lblNumeroNavigatori = new JLabel("Numero navigatori");
		GridBagConstraints gbc_lblNumeroNavigatori = new GridBagConstraints();
		gbc_lblNumeroNavigatori.anchor = GridBagConstraints.WEST;
		gbc_lblNumeroNavigatori.insets = new Insets(0, 0, 5, 5);
		gbc_lblNumeroNavigatori.gridx = 1;
		gbc_lblNumeroNavigatori.gridy = 2;
		add(lblNumeroNavigatori, gbc_lblNumeroNavigatori);
		
		numNavigatoriTextField = new JFormattedTextField();
		numNavigatoriTextField.setEditable(false);
		numNavigatoriTextField.setText("0");
		GridBagConstraints gbc_numNavigatoriTextField = new GridBagConstraints();
		gbc_numNavigatoriTextField.insets = new Insets(0, 0, 5, 5);
		gbc_numNavigatoriTextField.fill = GridBagConstraints.HORIZONTAL;
		gbc_numNavigatoriTextField.gridx = 3;
		gbc_numNavigatoriTextField.gridy = 2;
		add(numNavigatoriTextField, gbc_numNavigatoriTextField);
		
		lblSelezionaNavigatore = new JLabel("Seleziona navigatore");
		GridBagConstraints gbc_lblSelezionaNavigatore = new GridBagConstraints();
		gbc_lblSelezionaNavigatore.anchor = GridBagConstraints.WEST;
		gbc_lblSelezionaNavigatore.insets = new Insets(0, 0, 5, 5);
		gbc_lblSelezionaNavigatore.gridx = 1;
		gbc_lblSelezionaNavigatore.gridy = 4;
		add(lblSelezionaNavigatore, gbc_lblSelezionaNavigatore);
		
		comboBox = new JComboBox();
		comboBox.addActionListener(this);
		comboBox.setSelectedIndex(-1);
		GridBagConstraints gbc_comboBox = new GridBagConstraints();
		gbc_comboBox.gridwidth = 3;
		gbc_comboBox.insets = new Insets(0, 0, 5, 0);
		gbc_comboBox.fill = GridBagConstraints.HORIZONTAL;
		gbc_comboBox.gridx = 2;
		gbc_comboBox.gridy = 4;
		add(comboBox, gbc_comboBox);
		
		btnGetdatanavigatore = new JButton(getDateImage);
		btnGetdatanavigatore.addActionListener(this);
		GridBagConstraints gbc_btnGetdatanavigatore = new GridBagConstraints();
		gbc_btnGetdatanavigatore.gridwidth = 5;
		gbc_btnGetdatanavigatore.insets = new Insets(0, 0, 5, 0);
		gbc_btnGetdatanavigatore.gridx = 0;
		gbc_btnGetdatanavigatore.gridy = 5;
		add(btnGetdatanavigatore, gbc_btnGetdatanavigatore);
		
		lblNavigatore = new JLabel("Navigatore");
		GridBagConstraints gbc_lblNavigatore = new GridBagConstraints();
		gbc_lblNavigatore.anchor = GridBagConstraints.WEST;
		gbc_lblNavigatore.insets = new Insets(0, 0, 5, 5);
		gbc_lblNavigatore.gridx = 1;
		gbc_lblNavigatore.gridy = 6;
		add(lblNavigatore, gbc_lblNavigatore);
		
		nomeNavigatoreField = new JFormattedTextField();
		nomeNavigatoreField.setEditable(false);
		GridBagConstraints gbc_nomeNavigatoreField = new GridBagConstraints();
		gbc_nomeNavigatoreField.gridwidth = 3;
		gbc_nomeNavigatoreField.insets = new Insets(0, 0, 5, 5);
		gbc_nomeNavigatoreField.fill = GridBagConstraints.HORIZONTAL;
		gbc_nomeNavigatoreField.gridx = 2;
		gbc_nomeNavigatoreField.gridy = 6;
		add(nomeNavigatoreField, gbc_nomeNavigatoreField);
		
		lblNumeroIncroci = new JLabel("Numero incroci");
		GridBagConstraints gbc_lblNumeroIncroci = new GridBagConstraints();
		gbc_lblNumeroIncroci.anchor = GridBagConstraints.WEST;
		gbc_lblNumeroIncroci.insets = new Insets(0, 0, 5, 5);
		gbc_lblNumeroIncroci.gridx = 1;
		gbc_lblNumeroIncroci.gridy = 8;
		add(lblNumeroIncroci, gbc_lblNumeroIncroci);
		
		numIncrociTextField = new JFormattedTextField();
		numIncrociTextField.setEditable(false);
		GridBagConstraints gbc_numIncrociTextField = new GridBagConstraints();
		gbc_numIncrociTextField.insets = new Insets(0, 0, 5, 5);
		gbc_numIncrociTextField.fill = GridBagConstraints.HORIZONTAL;
		gbc_numIncrociTextField.gridx = 3;
		gbc_numIncrociTextField.gridy = 8;
		add(numIncrociTextField, gbc_numIncrociTextField);
		
		lblNumeroStrade = new JLabel("Numero strade");
		GridBagConstraints gbc_lblNumeroStrade = new GridBagConstraints();
		gbc_lblNumeroStrade.anchor = GridBagConstraints.WEST;
		gbc_lblNumeroStrade.insets = new Insets(0, 0, 5, 5);
		gbc_lblNumeroStrade.gridx = 1;
		gbc_lblNumeroStrade.gridy = 10;
		add(lblNumeroStrade, gbc_lblNumeroStrade);
		
		numStradeTextField = new JFormattedTextField();
		numStradeTextField.setEditable(false);
		GridBagConstraints gbc_numStradeTextField = new GridBagConstraints();
		gbc_numStradeTextField.insets = new Insets(0, 0, 5, 5);
		gbc_numStradeTextField.fill = GridBagConstraints.HORIZONTAL;
		gbc_numStradeTextField.gridx = 3;
		gbc_numStradeTextField.gridy = 10;
		add(numStradeTextField, gbc_numStradeTextField);
		
		lblNumeroAuto = new JLabel("Numero auto");
		GridBagConstraints gbc_lblNumeroAuto = new GridBagConstraints();
		gbc_lblNumeroAuto.anchor = GridBagConstraints.WEST;
		gbc_lblNumeroAuto.insets = new Insets(0, 0, 0, 5);
		gbc_lblNumeroAuto.gridx = 1;
		gbc_lblNumeroAuto.gridy = 12;
		add(lblNumeroAuto, gbc_lblNumeroAuto);
		
		numAutoTextField = new JFormattedTextField();
		numAutoTextField.setEditable(false);
		GridBagConstraints gbc_numAutoTextField = new GridBagConstraints();
		gbc_numAutoTextField.insets = new Insets(0, 0, 0, 5);
		gbc_numAutoTextField.fill = GridBagConstraints.HORIZONTAL;
		gbc_numAutoTextField.gridx = 3;
		gbc_numAutoTextField.gridy = 12;
		add(numAutoTextField, gbc_numAutoTextField);
		
		
		updatePanel();
		
	}

	
	
	public void updatePanel()
	{
		
			if(comboBox.getSelectedIndex() != -1)
			{
				
				int indexSelected = comboBox.getSelectedIndex();
				
				nomeNavigatoreField.setText(broker.getNav()[indexSelected].getLocalName());
				numIncrociTextField.setText(broker.getNav()[indexSelected].getSubGraph().vertexSet().size()+"");
				numStradeTextField.setText(broker.getNav()[indexSelected].getSubGraph().edgeSet().size()+"");
				numAutoTextField.setText(broker.getNav()[indexSelected].getAutos().size()+"");
				
				comboBox.setSelectedIndex(-1);
				
			}
			else if(broker.getNav() != null)
				JOptionPane.showMessageDialog(null, "Devi selezionare uno dei navigatori!", "Attenzione!", JOptionPane.WARNING_MESSAGE);
			
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		
		if(e.getSource() == btnGetdatanavigatore)
		{
			updatePanel();
		}
		
	}
	
	
	
	@Override
	public void update(Object object) {
		// TODO Auto-generated method stub

		if(object.equals("loadnav"))
		{
			numNavigatoriTextField.setText(broker.getNav().length+"");

			comboBox.removeAllItems();

			for (int i = 0; i < broker.getNav().length; i++) {
				comboBox.addItem(broker.getNav()[i].getLocalName());
			}

			comboBox.setSelectedIndex(-1);
		}

	}


	
}
